<template>
  <div class="login-wrapper">
    <div class="login-container">
      <h2>登录</h2>
      <form @submit.prevent="login">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" id="username" v-model="username" required />
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="password" required />
        </div>
        <button type="submit">登录</button>
      </form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import qs from 'qs'; // 引入 qs 库

export default {
  data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    async login() {
      try {
        const response = await axios.post(
          'http://localhost:8080/api/login',
          qs.stringify({
            username: this.username,
            password: this.password,
          }),
          {
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
            },
            withCredentials: true,
          }
        );
        if (response.data.flag) {
          // 存储 JWT
          localStorage.setItem('jwt', response.data.token);
           // 清除健康数据提交状态
        localStorage.removeItem('healthDataSubmitted');
          this.$router.push('/home');
        } else {
          alert(response.data.message);
        }
      } catch (error) {
        alert('登录出错，请稍后重试');
      }
    },
  },
};
</script>

<style scoped>
body, html {
  height: 100%;
  margin: 0;
  font-family: Arial, sans-serif;
  background-color: #f0f2f5;
}

.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('/background.jpg');
  background-size: cover; 
  background-position: center;
  background-repeat: no-repeat; 
}

.login-container {
  max-width: 400px;
  width: 100%;
  padding: 20px; /* 减少留白部分 */
  background-color: rgba(255, 255, 255, 0.8); /* 白色背景，透明度为 0.8 */
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.login-container h2 {
  margin-bottom: 15px; /* 调整标题与表单的间距 */
  font-size: 24px;
  color: #333;
}

.form-group {
  margin-bottom: 15px; /* 调整表单组之间的间距 */
  text-align: left;
}

label {
  display: block;
  margin-bottom: 5px;
  font-size: 14px;
  color: #555;
  width: 80%; /* 设置与输入框相同的宽度 */
  margin-left: auto; /* 左对齐 */
  margin-right: auto; /* 左对齐 */
}

input {
  width: 80%; /* 调整输入框宽度 */
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
  box-sizing: border-box;
  margin: 0 auto; /* 使输入框居中 */
  display: block; /* 使输入框独占一行 */
}

input:focus {
  border-color: #42b983;
  outline: none;
}

button {
  width: 80%; /* 调整按钮宽度 */
  padding: 12px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  margin: 0 auto; /* 使按钮居中 */
  display: block; /* 使按钮独占一行 */
}

button:hover {
  background-color: #369f6e;
}
</style>